﻿/// <summary>
// DBLinq.MySqlProviders - .NET Providers for MySql using the framework DBLinq under MIT license, author Jiri George Moudry
// http://linq.to/db
// and the CodeFx.MySqlProviders under GNU licence, author Laurent Morisseau
// http://www.codeplex.com/CodeFxMySqlProvider
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// Laurent Morisseau
/// </summary>
/// <author><a href="mailto:laurent.morisseau@gmail.com">Laurent Morisseau</a></author>
/// 
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Web.Security;
using System;
using MySql.Data.MySqlClient;

namespace DBLinq.MySqlProviders.TestProject
{
    
    
    /// <summary>
    ///This is a test class for AspNetMembershipsDTOTest and is intended
    ///to contain all AspNetMembershipsDTOTest Unit Tests
    ///</summary>
	[TestClass ()]
	public class AspNetMembershipsDTOTest
	{

        private MySqlConnection conn;
        private uint appid = 0;
        private uint userid = 0;
		private TestContext testContextInstance;

		/// <summary>
		///Gets or sets the test context which provides
		///information about and functionality for the current test run.
		///</summary>
		public TestContext TestContext
		{
			get
			{
				return testContextInstance;
			}
			set
			{
				testContextInstance = value;
			}
		}

		#region Additional test attributes
		[ClassInitialize ()]
		public static void MyClassInitialize( TestContext testContext )
		{

		}
		//
		//Use ClassCleanup to run code after all tests in a class have run
		[ClassCleanup ()]
		public static void MyClassCleanup()
		{
			MySqlConnection conn = TestHelper.Open ();
			try
			{
				TestHelper.DeleteMemberTest ( conn );
			}
			catch ( Exception ex )
			{
				Assert.Fail ( ex.Message );
			}
			finally
			{
				TestHelper.Close ( conn );
			}
		}
		//
		//Use TestInitialize to run code before running each test
		[TestInitialize ()]
		public void MyTestInitialize()
		{
			ConnectionString.Current.CurrentString = TestHelper.connectionstring;
			conn = TestHelper.Open ();
			TestHelper.DeleteMemberTest ( conn );
            TestHelper.DeleteApplicationTest(conn);
            appid = TestHelper.InsertApplicationTest(conn);

		}
		//
		//Use TestCleanup to run code after each test has run
		[TestCleanup ()]
		public void MyTestCleanup()
        {
            TestHelper.DeleteMemberTest(conn);
            TestHelper.DeleteApplicationTest(conn);
			TestHelper.Close ( conn );
		}

		#endregion


		/// <summary>
		///A test for Update_LastLoginDate
		///</summary>
		[TestMethod ()]
		public void Update_LastLoginDateTest()
		{
			/// Init
			userid = TestHelper.InsertMemberTest ( conn,appid );

            /// Test
            AspNetMembership member = AspNetMembershipsDTO.Select(userid);
			Assert.IsNotNull ( member );
			DateTime initial = member.LastLoginDate.Value ;

			/// Assert
			AspNetMembershipsDTO.Update_LastLoginDate ( member );
            member = AspNetMembershipsDTO.Select(userid);
			Assert.IsNotNull ( member );
			Assert.AreNotEqual<DateTime> ( member.LastLoginDate.Value, initial );

			try
			{
				MySqlCommand cmd = new MySqlCommand ( "SELECT LastLoginDate FROM AspNet_Membership WHERE userid=?userid", conn );
                cmd.Parameters.Add(new MySqlParameter("?userid", userid));
				object reader = cmd.ExecuteScalar ();
				Assert.IsNotNull ( reader );
				Assert.AreEqual ( member.LastLoginDate.Value, reader);
			}
			catch ( Exception ex )
			{
				Assert.Fail ( ex.Message );
			}
			finally
			{
				TestHelper.DeleteMemberTest  ( conn );
			}
		}

		/// <summary>
		///A test for Update
		///</summary>
		[TestMethod ()]
		public void UpdateTest()
		{
			/// Init
			userid = TestHelper.InsertMemberTest ( conn, appid  );
            
			/// Test
            AspNetMembership member = AspNetMembershipsDTO.Select(userid);
			Assert.IsNotNull ( member );
			DateTime initial = member.LastLoginDate.Value;
			DateTime excepted = DateTime.UtcNow ;
			member.LastLoginDate = excepted;
			/// Assert
			AspNetMembershipsDTO.Update( member );
            member = AspNetMembershipsDTO.Select(userid);
            Assert.IsNotNull(member);
            Assert.AreNotEqual<DateTime>(member.LastLoginDate.Value, initial);
			Assert.IsTrue( member.LastLoginDate.Value.CompareTo(excepted)<0);

			try
			{
				MySqlCommand cmd = new MySqlCommand ( "SELECT LastLoginDate FROM AspNet_Membership WHERE userid=?userid", conn );
                cmd.Parameters.Add(new MySqlParameter("?userid", userid));
				object reader = cmd.ExecuteScalar ();
				Assert.IsNotNull ( reader );
				Assert.AreEqual ( member.LastLoginDate.Value, reader );
			}
			catch ( Exception ex )
			{
				Assert.Fail ( ex.Message );
			}
			finally
			{
				TestHelper.DeleteMemberTest ( conn );
			}
		
		}

		/// <summary>
		///A test for SetPassword
		///</summary>
		[TestMethod ()]
		public void SetPasswordTest()
		{
			/// Init
			userid = TestHelper.InsertMemberTest ( conn, appid  );
            
			/// Test
			string NewPassword = "NewPassword"; 
			bool PasswordFormat = true; 
			string PasswordSalt = "NewPassword";
            AspNetMembershipsDTO.SetPassword(NewPassword, PasswordFormat, PasswordSalt, userid);

			/// Assert
			MySqlDataReader reader =null;
			
			try
			{
				MySqlCommand cmd = new MySqlCommand ( "SELECT * FROM AspNet_Membership WHERE userid=?userid", conn );
                cmd.Parameters.Add(new MySqlParameter ("?userid",userid));
				reader = cmd.ExecuteReader  ();
				Assert.IsNotNull ( reader );
				Assert.IsTrue ( reader .Read());
				Assert.AreEqual<string> ( reader.GetString ( 2 ), NewPassword );
				Assert.AreEqual<string> ( reader.GetString ( 4 ), PasswordSalt );
			}
			catch ( Exception ex )
			{
				Assert.Fail ( ex.Message );
			}
			finally
			{
				if ( reader != null ) reader.Close ();
				TestHelper.DeleteMemberTest ( conn );
			}
		}

		/// <summary>
		///A test for SelectByFk_ApplicationId_UserName
		///</summary>
		[TestMethod ()]
		public void SelectByFk_ApplicationId_UserNameTest()
		{
			/// Init
            userid = TestHelper.InsertMemberTest(conn, appid);
			
			/// Test
			AspNetMembership actual = AspNetMembershipsDTO.SelectByFk_ApplicationId_UserName ( appid, TestHelper.UserName );
			Assert.AreEqual ( userid , actual.UserId  );

			
		}

		/// <summary>
		///A test for Select
		///</summary>
		[TestMethod ()]
		public void SelectTest()
		{
			/// Init
			userid = TestHelper.InsertMemberTest ( conn , appid );
            
			/// Test
            AspNetMembership actual = AspNetMembershipsDTO.Select(userid);
			Assert.IsNotNull ( actual );
            Assert.AreEqual<uint>(userid, actual.UserId);

			/// Delete 
			TestHelper.DeleteMemberTest ( conn );
		}

		/// <summary>
		///A test for GetUserNameByEmail
		///</summary>
		[TestMethod ()]
		public void GetUserNameByEmailTest()
		{
			/// Init
            userid = TestHelper.InsertMemberTest(conn, appid);
			
			/// Test
            string actual = AspNetMembershipsDTO.GetUserNameByEmail(appid , TestHelper.Email);
			Assert.IsNotNull ( actual );
			Assert.AreEqual<string> ( TestHelper.UserName, actual );			

			
		}

		/// <summary>
		///A test for ExistMultipleEmail
		///</summary>
		[TestMethod ()]
		public void ExistMultipleEmailTest()
		{
            bool expected = false; // TODO: Initialize to an appropriate value
			bool actual = AspNetMembershipsDTO.ExistMultipleEmail ( appid, 0, TestHelper.Email );
			Assert.AreEqual ( expected, actual );
            userid = TestHelper.InsertMemberTest(conn, appid);

            expected = true;
            actual = AspNetMembershipsDTO.ExistMultipleEmail(appid, 0, TestHelper.Email);
            expected = false;
            actual = AspNetMembershipsDTO.ExistMultipleEmail(appid, userid , TestHelper.Email);
            Assert.AreEqual(expected, actual);
            
		}

		/// <summary>
		///A test for Exist
		///</summary>
		[TestMethod ()]
		public void ExistTest1()
		{
            bool expected = false; // TODO: Initialize to an appropriate value
			bool actual;
			actual = AspNetMembershipsDTO.Exist ( 0 );
			Assert.AreEqual ( expected, actual );

            userid = TestHelper.InsertMemberTest(conn, appid);
            expected = true; // TODO: Initialize to an appropriate value
            actual = AspNetMembershipsDTO.Exist(userid);
            Assert.AreEqual(expected, actual);

            
		}

		/// <summary>
		///A test for Exist
		///</summary>
		[TestMethod ()]
		public void ExistTest()
        {
            bool expected = false; // TODO: Initialize to an appropriate value
            bool actual;
            actual = AspNetMembershipsDTO.Exist(appid, TestHelper.Email);
            Assert.AreEqual(expected, actual);

            TestHelper.InsertMemberTest(conn, appid);
            expected = true; // TODO: Initialize to an appropriate value
            actual = AspNetMembershipsDTO.Exist(appid, TestHelper.Email );
            Assert.AreEqual(expected, actual);

		}

		/// <summary>
		///A test for DeleteByFk_UserId
		///</summary>
		[TestMethod ()]
		public void DeleteByFk_UserIdTest()
		{
            userid = TestHelper.InsertMemberTest(conn, appid);
            
            AspNetMembershipsDTO.DeleteByFk_UserId(userid);

            Assert.IsTrue(TestHelper.ExistMemberTest(conn, appid));
		}

		/// <summary>
		///A test for CustomUpdate
		///</summary>
		[TestMethod ()]
		public void CustomUpdateTest()
		{
            string NewComment = "NewComment";
            /// Init
            userid = TestHelper.InsertMemberTest(conn, appid);
            
            /// Test
            AspNetMembership member = AspNetMembershipsDTO.Select(userid);
            Assert.IsNotNull(member);
            member.Comment = NewComment;
            /// Assert
            AspNetMembershipsDTO.CustomUpdate(member);
            member = AspNetMembershipsDTO.Select(userid);
            Assert.IsNotNull(member);
            Assert.AreEqual<string>(member.Comment, NewComment);

            try
            {
                MySqlCommand cmd = new MySqlCommand("SELECT Comment FROM AspNet_Membership WHERE userid=?userid", conn);
                cmd.Parameters.Add(new MySqlParameter("?userid", userid));
                object reader = cmd.ExecuteScalar();
                Assert.IsNotNull(reader);
                Assert.AreEqual(member.Comment, reader);
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }
            finally
            {
                TestHelper.DeleteMemberTest(conn);
            }
		
		}

		/// <summary>
		///A test for CountAllUsersByEmail
		///</summary>
		[TestMethod ()]
		public void CountAllUsersByEmailTest()
		{
            MySqlCommand cmd = new MySqlCommand("select count(*) from AspNet_Membership where email=?email and applicationid=?appid", conn);
            cmd.Parameters.Add(new MySqlParameter("?email", TestHelper.Email));
            cmd.Parameters.Add(new MySqlParameter("?appid", appid));
            object expected = cmd.ExecuteScalar();
                
			object actual;
			actual = AspNetMembershipsDTO.CountAllUsersByEmail ( appid, TestHelper.Email );
			Assert.AreEqual<string> ( expected.ToString(), actual.ToString() );

            TestHelper.InsertMemberTest(conn, appid);

             cmd = new MySqlCommand("select count(*) from AspNet_Membership where email=?email and applicationid=?appid", conn);
            cmd.Parameters.Add(new MySqlParameter("?email", TestHelper.Email));
            cmd.Parameters.Add(new MySqlParameter("?appid", appid));
            expected = cmd.ExecuteScalar();

            actual = AspNetMembershipsDTO.CountAllUsersByEmail(appid, TestHelper.Email);
            Assert.AreEqual<string>(expected.ToString(), actual.ToString());

        }

		/// <summary>
		///A test for CountAllUsers
		///</summary>
		[TestMethod ()]
		public void CountAllUsersTest1()
		{
            MySqlCommand cmd = new MySqlCommand("select count(*) from ASPNET_users where username=?username and applicationid=?appid", conn);
            cmd.Parameters.Add(new MySqlParameter("?username", TestHelper.UserName));
            cmd.Parameters.Add(new MySqlParameter("?appid", appid));
            object expected = cmd.ExecuteScalar();
            
            object actual;
			actual = AspNetMembershipsDTO.CountAllUsers ( appid, TestHelper.UserName );
			Assert.AreEqual<string> ( expected.ToString(), actual.ToString() );

            TestHelper.InsertMemberTest(conn, appid);

            cmd = new MySqlCommand("select count(*) from ASPNET_users where username=?username and applicationid=?appid", conn);
            cmd.Parameters.Add(new MySqlParameter("?username", TestHelper.UserName));
            cmd.Parameters.Add(new MySqlParameter("?appid", appid));
            expected = cmd.ExecuteScalar();

            actual = AspNetMembershipsDTO.CountAllUsers(appid, TestHelper.UserName);
            Assert.AreEqual<string>(expected.ToString(), actual.ToString());

		}

		/// <summary>
		///A test for CountAllUsers
		///</summary>
		[TestMethod ()]
		public void CountAllUsersTest()
		{
            MySqlCommand cmd = new MySqlCommand("select count(*) from AspNet_Membership where applicationid=?appid", conn);
            cmd.Parameters.Add(new MySqlParameter("?appid", appid));
            object expected = cmd.ExecuteScalar();
            
            object actual;
			actual = AspNetMembershipsDTO.CountAllUsers ( appid);
			Assert.AreEqual<string> ( expected.ToString(), actual.ToString() );

            TestHelper.InsertMemberTest(conn, appid);

            cmd = new MySqlCommand("select count(*) from AspNet_Membership where applicationid=?appid", conn);
            cmd.Parameters.Add(new MySqlParameter("?appid", appid));
            expected = cmd.ExecuteScalar();

            actual = AspNetMembershipsDTO.CountAllUsers(appid);
            Assert.AreEqual<string>(expected.ToString(), actual.ToString());
		}

		/// <summary>
		///A test for CoreAssign
		///</summary>
		[TestMethod ()]
		[DeploymentItem ( "DBLinq.MySqlProviders.dll" )]
		public void CoreAssignTest()
		{
            AspNetMembership dataObject = new AspNetMembership ();
            dataObject.ApplicationId = 1;
			dataObject.Password = "1";
			dataObject.PasswordFormat = true;
			dataObject.PasswordSalt = "2";
			dataObject.MobilePin = "3";
			dataObject.Email = "4";
			dataObject.PasswordQuestion = "5";
			dataObject.PasswordAnswer = "6";
			dataObject.IsApproved = true;
			dataObject.IsLockedOut = true;
			dataObject.CreateDate = DateTime.Now;
            dataObject.LastLoginDate = DateTime.Now;
			dataObject.LastPasswordChangedDate = DateTime.Now;
			dataObject.LastLockedOutDate = DateTime.Now;
			dataObject.FailedPasswordAttemptCount = 1;
			dataObject.FailedPasswordAttemptWindowStart = DateTime.Now;
			dataObject.FailedPasswordAnswerAttemptCount = 1;
			dataObject.FailedPasswordAnswerAttemptWindowStart = DateTime.Now;
			dataObject.Comment = "7";
            AspNetMembership ToAssign = new AspNetMembership(); 
            AspNetMembershipsDTO_Accessor.CoreAssign(dataObject, ToAssign);
            Assert.AreEqual<uint>(dataObject.ApplicationId, ToAssign.ApplicationId);
            Assert.AreEqual<string>(dataObject.Password , ToAssign.Password);
		}

		/// <summary>
		///A test for ChangePasswordQuestionAndAnswer
		///</summary>
		[TestMethod ()]
		public void ChangePasswordQuestionAndAnswerTest()
		{
			/// Init
            string NewPasswordQuestion = "NewPasswordQuestion"; // TODO: Initialize to an appropriate value
            string NewPasswordAnswer = "NewPasswordAnswer"; // TODO: Initialize to an appropriate value

            userid = TestHelper.InsertMemberTest(conn, appid);
            			
            /// Assert
            AspNetMembershipsDTO.ChangePasswordQuestionAndAnswer(NewPasswordQuestion, NewPasswordAnswer, userid);
            AspNetMembership member = AspNetMembershipsDTO.Select(userid);
            Assert.IsNotNull(member);
            Assert.AreEqual<string>(member.PasswordQuestion, NewPasswordQuestion);
            Assert.AreEqual<string>(member.PasswordAnswer, NewPasswordAnswer);

            try
            {
                MySqlCommand cmd = new MySqlCommand("SELECT PasswordQuestion FROM AspNet_Membership WHERE userid=?userid", conn);
                cmd.Parameters.Add(new MySqlParameter("?userid", userid));
                object reader = cmd.ExecuteScalar();
                Assert.IsNotNull(reader);
                Assert.AreEqual<string>(NewPasswordQuestion, reader.ToString());

                cmd = new MySqlCommand("SELECT PasswordAnswer FROM AspNet_Membership WHERE userid=?userid", conn);
                cmd.Parameters.Add(new MySqlParameter("?userid", userid));
                reader = cmd.ExecuteScalar();
                Assert.IsNotNull(reader);
                Assert.AreEqual<string>(NewPasswordAnswer, reader.ToString());
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }
            finally
            {
                TestHelper.DeleteMemberTest(conn);
            }
		
		}
	}
}
